package com.westos.todo.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.westos.todo.constant.Constance;
import com.westos.todo.dto.ApiResult;
import com.westos.todo.entity.Todo;
import com.westos.todo.entity.TodoClass;
import com.westos.todo.page.TodoClassVO;
import com.westos.todo.page.TodoVO;
import com.westos.todo.service.TodoService;

@RequestMapping("/todo")
@Controller("todoController")
public class TodoController {
	
	@Autowired
	private TodoService todoService;
	
	// ../todo/selecttodo.do
	@RequestMapping(value="/selecttodo",produces={MediaType.APPLICATION_JSON_VALUE+";charset=utf-8"})
	@ResponseBody
	public ApiResult<TodoVO> selecttodo(){
		TodoVO todoVO=new TodoVO();
		List<TodoClassVO> result=todoService.listtoclasses(null);
		todoVO.setTodoclasses(result);
		
		ApiResult<TodoVO> apiresult=new ApiResult<TodoVO>();
		apiresult.setData(todoVO);
		apiresult.setStatus(Constance.TODO_LOGIN);
		
		return apiresult;
	}
	
	// ../todo/savetodoclass.do
	@RequestMapping("/savetodoclass")
	public void savetodoclass(HttpServletRequest req,HttpServletResponse resp){
		String todoClassName=req.getParameter("todoClassName");
		TodoClass todoClass=new TodoClass();
		todoClass.setName(todoClassName);
		todoService.saveTodoClass(todoClass);
	}
	
	// ../todo/savetodoclass.do
	@RequestMapping("/savetodo")
	public void savetodo(HttpServletRequest req,HttpServletResponse resp){
		String title=req.getParameter("title");
		int todoclassid=Integer.parseInt(req.getParameter("todoclassid"));
		Todo todo=new Todo();
		todo.setCreate_dt(new Date());
		todo.setState(0);
		todo.setTitle(title);
		todo.setTodoclassid(todoclassid);
		todoService.saveTodo(todo);
	}
	
	//  ../todo/updatetodostate.do
	@RequestMapping("/updatetodostate")
	public void updatetodostate(HttpServletRequest req,HttpServletResponse resp){
		Integer id=Integer.parseInt(req.getParameter("id"));
		todoService.updateTodo(id);
	}
	
	// ../todo/removetodo.do
	@RequestMapping("/removetodo")
	public void removetodo(HttpServletRequest req,HttpServletResponse resp){
		Integer id=Integer.parseInt(req.getParameter("id"));
		todoService.removeTodo(id);
	}
	
	
	
}
